package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Landlord;
import com.qf.service.LandlordService;
import com.qf.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
public class LandlordController {
    private Logger logger = LogManager.getLogger(LandlordController.class);
    @Autowired
    private LandlordService service;

    /**
     * 模糊分页查询房东信息
     * @return
     */
    @GetMapping("/landlords")
    public R findAll(Landlord landlord) {
        logger.debug("Landlord ==> {}", landlord);
        PageHelper.startPage(landlord.getPageNum(), landlord.getPageSize());
        List<Landlord> landlords = service.findAll(landlord);
        PageInfo<Landlord> pageInfo = new PageInfo<>(landlords);
        return R.ok(pageInfo);
    }

    /**
     * 添加房东信息
     */
    @PostMapping("/add")
    public R addLandlord(@RequestBody Landlord landlord) {
        service.addLandlord(landlord);
        return R.ok();
    }
    /**
     * 修改房东信息
     */
    @PostMapping ("/update")
    public R updateLandlord(@RequestBody Landlord landlord) {
        service.updateLandlord(landlord);
        return R.ok();
    }

    /**
     * 删除房东信息
     */
    @GetMapping("/delete")
    public R deleteLandlord(Integer id) {
        service.deleteLandlord(id);
        return R.ok();
    }

    /**
     * 批量删除房东信息
     */
    @PostMapping("/deletes")
    public R batchDeleteLandlord(@RequestBody List<Integer> ids) {
        Collections.sort(ids, Collections.reverseOrder());
        for (Integer id : ids) {
            service.deleteLandlord(id);
        }
        return R.ok();
    }
}
